package com.itheima.d4_stringbuilder;

//目标：掌握StringBuilder常用方法的使用
public class Demo2 {
    public static void main(String[] args) {
        //调用StringBuilder空参构造创建对象
        StringBuilder sb1 = new StringBuilder();
        System.out.println(sb1); // ""

        //调用StringBuilder的有参构造创建对象（把String对象转成StringBuilder对象）
        StringBuilder sb2 = new StringBuilder("abc");
        System.out.println(sb2);
        System.out.println("---------------");

        //1.public StringBuilder append(任意类型): 往StringBuilder容器中添加元素，并返回StringBuilder容器本身
        StringBuilder sb3 = sb1.append(1);
        System.out.println("sb3和sb1是否是同一个对象：" + (sb1 == sb3));
        System.out.println(sb1);

        sb1.append(2.2);
        System.out.println(sb1);

        //方法返回的对象还是StringBuilder对象，就可以链式编程，继续调用StringBuilder提供的方法
        sb1.append(true).append('中').append("abc");
        System.out.println(sb1);
        System.out.println("---------------");

        //2.public StringBuilder reverse(): 将StringBuilder容器中的内容反转，并返回StringBuilder容器本身
        StringBuilder sb4 = sb2.reverse();
        System.out.println("sb2和sb4是否是同一个对象：" + (sb2 == sb4));
        System.out.println(sb2);
        System.out.println("---------------");

        //3.public StringBuilder toString(): 将StringBuilder转换为String
        String str = sb2.toString();
        System.out.println(str);



    }
}
